
*********************************************************************************;

/* 				This file provides the results shown in Table 6					*/
/*				Regressions of market returns on passive flows					*/

*********************************************************************************;

libname rep 'C:\Users\~';

data regbase; set rep.mydata; run;


***** PanelA: Regressions of market on passive flows ;

option missing = " ";
%macro print;
data N; set N; Estimate = nValue1; if Label1 = "Used"; run;
data Nobs; set N; Parameter = "N"; keep EstType Parameter Estimate; run;
data r2; set fit; Parameter = "RSQ"; keep EstType Parameter RSquare; rename RSquare = Estimate;  run;
data final; set parest Nobs r2 ; drop EstType StdErr Probt DF; run;
data final&j; set final; rename Estimate = &yvar tValue = t_&yvar; 
if Parameter not in ("p_freq", "N", "RSQ") then delete; run;
%mend;

%let var1 = mktrf; 
%let var2 = sprf;


%macro passive ;

%do i=1 %to 2;
%if &i =1 %then %do; %let freq = low; %let con = high; %end; 
%else %do; %let freq = high; %let con = low; %end;
%do j=1 %to 2;
%let temp = &&var&j;
%let yvar = &&temp._&freq;
%put &yvar;
ods output "Nonlinear GMM Summary of Residual Errors" = fit;
ods output "Nonlinear GMM Parameter Estimates" = parest;
ods output "Estimation Summary Statistics" = N;
proc model data=regbase plot =none;
endo &yvar ; exog passive_low passive_high amihud tover hml smb; ; instruments _exog_;
&yvar=b0 +p_freq*passive_&freq +p_con*passive_&con + b2*amihud +b3*tover +b4*hml +b5*smb; 
fit &yvar / gmm kernel=(bart,13,0) vardef=n; run; quit; 
%print;
%end;
data merged&i; merge final1 final2 ; run;
%end;

data PanelA; merge merged1 merged2 ; run;
%mend;

%passive;

proc print data = PanelA noobs; run;


**** Panel B: Economic Significance;

proc means data = regbase noprint; 
output out = std_temp 
std(mktrf_low mktrf_high sprf_low sprf_high passive_low passive_high) = mktrf_low mktrf_high sprf_low sprf_high passive_low passive_high; run;

proc transpose data =std_temp out = std; run;

data std; set std; rename col1 = std;
if _name_ in ("_TYPE_", "_FREQ_") then delete;
var =  substr(_name_, 1, find(_name_, '_') -1);
freq = substr(_name_, find(_name_, '_') + 1); 
drop _name_ _label_; run;

data std; set std;
if var = "mktrf" then rtype = "MKT";
else if var = "sprf" then rtype = "SP";
else rtype = ""; run;

data temp; set PanelA; keep mktrf_low sprf_low mktrf_high sprf_high ;
if Parameter = "p_freq"; run;

proc transpose data = temp out=temp2; run;

data betas; set temp2; 
rename col1 = beta;
freq = substr(_name_, find(_name_, '_') + 1);
var = substr(_name_, 1, find(_name_, '_') -1);
drop _name_ _label_ ; run;

data betas; set betas;
if var = "mktrf" then rtype = "MKT";
else if var = "sprf" then rtype = "SP";
else rtype = ""; 
drop var; run;

data betas; retain rtype freq beta;
set betas; run;

proc sql;
create table betas2 as select a.*, b.std as std_fund, c.std as std_ret
from betas as a, std as b, std as c 
where a.freq = b.freq and b.var ="passive" and a.freq = c.freq and a.rtype = c.rtype
order by rtype, freq desc; quit;

proc sql;
create table betas3 as select *, sum(std_ret**2) as var_ttl
from betas2 group by rtype; quit;

data PanelB; set betas3;
R2_freq = (beta*std_fund/std_ret)**2; 
VarRatio = std_ret**2/var_ttl;
R2_ttlret = R2_freq*VarRatio;
EMag = beta*std_fund/std_ret;
drop var_ttl;
run;

proc print data = PanelB noobs; run;

